package cn.afactory.tygz.ui.base.views.search;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.ui.base.adapter.BaseListAdapter;
import cn.afactory.tygz.ui.base.entity.SearchHistoryEntity;

public class SearchHistoryView extends RelativeLayout {

    private List<SearchHistoryEntity> mSearchHistory = new ArrayList<SearchHistoryEntity>();
    private ListView search_history_rv;
    private SearchHistoryAdapter mAdapter;
    private TextView clear_search_history_btn;
    private OnViewClickListener mListener;

    public SearchHistoryView(Context context) {
        super(context);
        initView();
    }

    public SearchHistoryView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public SearchHistoryView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    private void initView() {
        View view = inflate(getContext(), R.layout.search_history_view, this);
        search_history_rv = (ListView) view.findViewById(R.id.search_history_rv);
        mAdapter = new SearchHistoryAdapter(getContext());
        search_history_rv.setAdapter(mAdapter);
        search_history_rv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mListener != null) {
                    mListener.OnItemClick(view, position, mAdapter.getItem(position));
                }
            }
        });
        clear_search_history_btn = (TextView) view.findViewById(R.id.clear_search_history_btn);
        clear_search_history_btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onCacheClear();
                }
            }
        });
    }


    public void setData(List<SearchHistoryEntity> data) {
        mSearchHistory.clear();
        if (data != null) {
            mSearchHistory.addAll(data);
        }
        mAdapter.addDataAll(data);
        mAdapter.notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnViewClickListener l) {
        mListener = l;
    }

    class SearchHistoryAdapter extends BaseListAdapter<SearchHistoryEntity, SearchHistoryAdapter.HistoryViewHolder> {

        public SearchHistoryAdapter(Context ctx) {
            super(ctx);
        }

        @Override
        public View newView(int position, LayoutInflater inflater, ViewGroup parent) {
            View view = View.inflate(mContext, R.layout.view_search_history_item_layout, null);
            return view;
        }

        @Override
        public HistoryViewHolder onViewHolderCreate(int position, View view) {
            HistoryViewHolder hvh = new HistoryViewHolder(view);
            return hvh;
        }

        @Override
        public void onBindViewHolder(int position, SearchHistoryEntity itemData, HistoryViewHolder historyViewHolder) {
            historyViewHolder.hot_keyword_item.setText(itemData.content);
        }

        class HistoryViewHolder extends BaseListAdapter.ViewHolder {
            private TextView hot_keyword_item;

            HistoryViewHolder(View view) {
                hot_keyword_item = (TextView) view.findViewById(R.id.hot_keyword_item);
            }
        }
    }

    public interface OnViewClickListener {
        void OnItemClick(View v, int position, SearchHistoryEntity item);

        void onCacheClear();
    }

}
